using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Linq.Models;

namespace Linq.Controllers;


public class Linqs:Controller
{
    // 找出数组中等于5的元素
    public ActionResult Index_1()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6 };
        var a=numbers.Where(x=>x==5);
        return View(a);
    }

    // 找出数组中在2到8之间的元素
    public ActionResult Index_2()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 9 };
        var a=numbers.Where(x=>x>=2 && x<=8);
        return View(a);
    }
    //将数组中的每个数字乘以2。 
      public ActionResult Index_3()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 9 };
        var a=numbers.Select(x=>x*2);
        return View(a);
    }

    // 找出所有名字以"王"开头的学生
      public ActionResult Index_4()
    {
        List<Student> students = new List<Student>
         {
    new Student {Id=1, Name = "王有才", Age = 21 },
    new Student {Id=2,  Name = "王中王", Age = 22 },
    new Student {Id=3,  Name = "张语嫣", Age = 23 },
    new Student {Id=4,  Name = "詹宇航", Age = 35 },
    new Student {Id=5,  Name = "郑雨良", Age = 26 },
         };
        //检查字符串是否以指定的前缀开始.StartsWith()
        var a=students.Where(x=>x.Name.StartsWith("王"));
        return View(a);
    }

    // 找出所有年龄大于20岁的学生，并按年龄降序排列。
    public ActionResult Index_5()
    {
        List<Student> students = new List<Student>
        {
           new Student {Id=1, Name = "王有才", Age = 21 },
           new Student {Id=2, Name = "罗婷", Age = 21 },
           new Student {Id=3,  Name = "王中王", Age = 22 },
           new Student {Id=4,  Name = "李子柒", Age = 22 },
           new Student {Id=5,  Name = "张语嫣", Age = 23 },
           new Student {Id=6,  Name = "詹宇航", Age = 35 },
           new Student {Id=7,  Name = "郑雨良", Age = 26 },
           new Student {Id=8,  Name = "欧文", Age = 26 },
        };
        // 降序排序的方法.OrderByDescending()
        var a=students.Where(x=>x.Age>20).OrderByDescending(x=>x.Age);
        return View(a);

    }
    // 查询并去重 找出数组中所有不重复的数字
    public ActionResult Index_6()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        var a = numbers.Distinct();
        // // GroupBy(n => n)：将数组中的数字按照值进行分组。
        //         .GroupBy(n => n)
        // // Where(g => g.Count() == 1)：筛选出只出现一次的组。
        //         .Where(g => g.Count() == 1)
        // // Select(g => g.Key)：选择这些组的键，即只出现一次的数字。
        //         .Select(g => g.Key);
        return View(a);
    }

    // 查询第一个元素 找出数组中第一个大于3的元素。
    public dynamic Index_7()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        // .First()返回第一个数
        var a=numbers.First(x=>x>3);
        
        return a.ToString();
    }
     
    //  查询最后一个元素 找出数组中最后一个小于7的元素。
    public dynamic Index_8()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        // .Last()返回最后一个数
        var a=numbers.Last(x=>x<7);
        return a.ToString();
    }
    
    // 查询元素是否存在 检查数组中是否存在大于10的元素。
    public dynamic Index_9()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        // var a =numbers.Where(x=>x>10);
        // All()所有元素都符合条件才返回True，有一个元素不符合就返回False
        // Any()只要有一个元素为符合就返回True，所有都不符合返回False
        var a=numbers.All(x=>x>10);
        return a;
    }
    
    // 查询元素的计数 计算数组中大于5的元素数量。
    public dynamic Index_10()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        var a=numbers.Count(x=>x>5);

        return a.ToString();
    }
    
    // 查询元素的总和 计算数组中所有元素的总和。
    public dynamic Index_11()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        // Sum()求总和
        var a=numbers.Sum(x=>x);
        return a.ToString();
    }
    
    // 查询元素的最大值 找出数组中的最大值。
    public dynamic Index_12()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        var a =numbers.Max(x=>x);
        return a.ToString();
    }

    // 查询元素的最小值 找出数组中的最小值。
    public dynamic Index_13()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        var a =numbers.Min(x=>x);
        return a.ToString();
    }

    // 查询元素的平均值 计算数组中所有元素的平均值。
    public dynamic Index_14()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        var a= numbers.Average(x=>x);
        return a.ToString();
    }

    // 查询特定条件的元素 找出数组中能被3整除的元素。
     public dynamic Index_15()
     {
        int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
        var a=numbers.Where(x=>x%3==0);
        return a;
     }

    //  查询并选择特定属性 找出所有学生的姓名和年龄
    public dynamic Index_16()
    {
            List<Student> students = new List<Student>
            {
                new Student {Id=1, Name = "王有才", Age = 21 },
                new Student {Id=2, Name = "罗婷", Age = 21 },
                new Student {Id=3,  Name = "王中王", Age = 22 },
                new Student {Id=4,  Name = "李子柒", Age = 22 },
                new Student {Id=5,  Name = "张语嫣", Age = 23 },
                new Student {Id=6,  Name = "詹宇航", Age = 35 },
                new Student {Id=7,  Name = "郑雨良", Age = 26 },
                new Student {Id=8,  Name = "欧文", Age = 26 },
            };
             var a=students.Select(x=>new{x.Name,x.Age});
             return a;

            // 扩展：
            // var a =students.Select(x=>{
            //     // 修改集合里面的名字
            //     var tmpName=x.Age<=22?$"{x.Name}青年":$"{x.Name}老登";
            //     // 年龄+1
            //     var tmpAge=x.Age+1;
                
            //     return new{Name=tmpName,tmpAge};
            // });
            // return a;
    }

    // 查询并分组 按年龄分组学生，并计算每个年龄组的学生数量。
    public dynamic Index_17()
    {
                    List<Student> students = new List<Student>
            {
                new Student {Id=1, Name = "王有才", Age = 21 },
                new Student {Id=2, Name = "罗婷", Age = 21 },
                new Student {Id=3,  Name = "王中王", Age = 22 },
                new Student {Id=4,  Name = "李子柒", Age = 22 },
                new Student {Id=5,  Name = "张语嫣", Age = 23 },
                new Student {Id=6,  Name = "詹宇航", Age = 35 },
                new Student {Id=7,  Name = "郑雨良", Age = 26 },
                new Student {Id=8,  Name = "欧文", Age = 26 },
            };
            var a=students.GroupBy(a=>a.Age).Select(b=>new{Age=b.Key,Count=b.Count()});
            return a;
    }

    // 查询并联结 联结学生和课程表，找出每个学生的所有课程。
    public dynamic Index_18()
    {
            List<Student> students = new List<Student>
        {
            new Student {Id=1, Name = "王有才", Age = 21 },
            new Student {Id=2, Name = "罗婷", Age = 21 },
            new Student {Id=3,  Name = "王中王", Age = 22 },
            new Student {Id=4,  Name = "李子柒", Age = 22 },
            new Student {Id=5,  Name = "张语嫣", Age = 23 },
            new Student {Id=6,  Name = "詹宇航", Age = 35 },
            new Student {Id=7,  Name = "郑雨良", Age = 26 },
            new Student {Id=8,  Name = "欧文", Age = 26 },
        };
        List<Course> courses=new List<Course>
        {
                new Course{StudentId=1,CourseName="英语"},
                new Course{StudentId=1,CourseName="数学"},
                new Course{StudentId=2,CourseName="语文"},
                new Course{StudentId=3,CourseName="物理"},
                new Course{StudentId=4,CourseName="化学"},
                new Course{StudentId=4,CourseName="生物"},
                new Course{StudentId=4,CourseName="语文"},
        };

        var a=students.Select(s=>
        {
            var b=courses.Where(x=>x.StudentId==s.Id).Select(x=>x.CourseName);
            // 将一个字符串集合，变成以逗号隔开的字符串
            var str=string.Join(",",b);
            var res=new{s.Id,s.Name,s.Age,CourseName=str};
                    return res;

        });
        // 把没有选课程的学生排除（不显示）
        var newa=a.Where(x=>x.CourseName.Length>0);
        return newa;
    }

    // 查询并反转 反转数组中的元素顺序。
    // public dynamic Index_19()
    // {

    // }

};